<?php

function show($a){
    echo '<pre>';
    print_r($a);
    echo '</pre>';
}

function show_log($data) {
	if(is_array($data) || is_object($data)){
		echo("<script>console.log('PHP: ".json_encode($data)."');</script>");
	} else {
		echo("<script>console.log('PHP: ".$data."');</script>");
	}
}

function redirect($url){
    ob_end_clean();
    header("location:$url");
    exit();
}

function validate($value, $condition, $min_len = 1, $max_len = 0){
	
	switch($condition){
		case DATA_REQUIRE:
		
			if(!$value)
				return false;
			else
				return true;
		case DATA_NUMERIC:
		
			if(preg_match("/[^0-9]+/",$value))
				return false;
			else
				return true;
				
		case DATA_EMAIL:
		
			if(filter_var($value, FILTER_VALIDATE_EMAIL))
				return true;
			else
				return false;
		
		case DATA_DATE:
		
			$date = DateTime::createFromFormat('d/m/Y', $value);
			return $date && $date->format('d/m/Y') == $value;
				
		case DATA_LIMIT_LENGTH:
		
			if(strlen($value) >= $min_len && ($max_len > 0 ? (strlen($value) <= $max_len ? true : false) : true))
				return true;
			else
				return false;
				
		case DATA_CUSTOM:
			$pattern = "/" . $min_len . "/";
			if(preg_match($pattern,$value))
				return true;
			else
				return false;
		default:
			return false;
	}
}

function showAlert($class,$message){
	echo "<div class='form-group boxAlert'><div class='alert $class col-xs-offset-3 col-xs-6'>";
		echo "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
		echo "$message";
	echo "</div></div>";
}
function unicode_convert($str){
  	        if(!$str) return false;
  	        $unicode = array(
                'a'=>array('á','à','ả','ã','ạ','ă','ắ','ặ','ằ','ẳ','ẵ','â','ấ','ầ','ẩ','ẫ','ậ'),
  	            'a'=>array('á','à','ả','ã','ạ','ă','ắ','ặ','ằ','ẳ','ẵ','â','ấ','ầ','ẩ','ẫ','ậ'),
  	            'd'=>array('đ'),
  	            'd'=>array('đ'),
  	            'e'=>array('é','è','ẻ','ẽ','ẹ','ê','ế','ề','ể','ễ','ệ'),
  	            'e'=>array('é','è','ẻ','ẽ','ẹ','ê','ế','ề','ể','ễ','ệ'),
  	            'i'=>array('í','ì','ỉ','ĩ','ị'),
  	            'i'=>array('í','ì','ỉ','ĩ','ị'),
  	            'o'=>array('ó','ò','ỏ','õ','ọ','ô','ố','ồ','ổ','ỗ','ộ','ơ','ớ','ờ','ở','ỡ','ợ'),
  	            '0'=>array('ó','ò','ỏ','õ','ọ','ô','ố','ồ','ổ','ỗ','ộ','ơ','ớ','ờ','ở','ỡ','ợ'),
  	            'u'=>array('ú','ù','ủ','ũ','ụ','ư','ứ','ừ','ử','ữ','ự'),
  	            'u'=>array('ú','ù','ủ','ũ','ụ','ư','ứ','ừ','ử','ữ','ự'),
  	            'y'=>array('ý','ỳ','ỷ','ỹ','ỵ'),
  	            'y'=>array('ý','ỳ','ỷ','ỹ','ỵ')
  	            //'-'=>array(' ','&quot;','.')
  	        );

  	        foreach($unicode as $nonunicode=>$uni){
  	        	foreach($uni as $value)
            	$str = @str_replace($value,$nonunicode,$str);
  	        }
    	return $str;
}
function compress_image($source_url, $destination_url, $quality) {
	$info = getimagesize($source_url);
 
	if($info['mime'] == 'image/jpeg' || $info['mime'] == 'image/jpg'){
		$image = imagecreatefromjpeg($source_url);
	}elseif($info['mime'] == 'image/gif'){
		$image = imagecreatefromgif($source_url);
	}elseif ($info['mime'] == 'image/png'){
		$image = imagecreatefrompng($source_url);
	}
 
	imagejpeg($image, $destination_url, $quality);//save it
 
	return $destination_url;
}